import Vue from 'vue';
import router from 'vue-router'; /* 导入路由 */
import Login from '../components/Login.vue'; /* 导入其他组件--【回头得改！】 */
import Reg from '../components/Reg.vue'; /* 导入其他组件--【回头得改！】 */
import Main from '../components/Main.vue';

import AdminList from '../views/back/admin/AdminList.vue';
import RoleList from '../views/back/role/RoleList.vue';
import SystemList from '../views/back/System/SystemList.vue';
import UsersList from '../views/back/Users/UsersList.vue';

import NewsType from "../views/back/newsType/NewsType";/*导入数据类型模块*/
import News from "../views/back/news/News.vue";/*导入数据类型模块*/
import Link from "../views/back/link/Link.vue";/*导入友情链接模块*/
import Comment from "../views/back/comment/Comment.vue";/*导入友情链接模块*/


import Index from "../views/front/Index.vue";
import NewsDetail from "../views/front/newsDetail.vue";

import UserLogin from "../views/user/userLogin.vue";
import UserReg from "../views/user/userReg.vue";
import UserPswUpdate from "../views/user/userPswUpdate.vue";


import UserCenter from "../views/user/UserCenter.vue";
import UserInfo from "../views/user/UserInfo.vue";
import UpdatePwd from "../views/user/UpdatePwd.vue";
import MyComment from "../views/user/MyComment.vue";
import MySC from "../views/user/MySC.vue";
import MyDZ from "../views/user/MyDZ.vue";
import Mynews from "../views/user/Mynews.vue";

import CreateNews from "../views/user/CreateNews";
import ElementUI from "element-ui";

Vue.use(router)



/* 定义组件路由 */
var rout = new router({
	routes: [{
		path: '/',
		component: Index
	},{
			path: '/index',
			component: Index
		},
		{
			path: '/newsDetail',
			component: NewsDetail
		},
		{
			path: '/userlogin',
			component: UserLogin
		},
		{
			path: '/userreg',
			component: UserReg
		},
		{
			path: '/userPswUpdate',
			component: UserPswUpdate
		},
		{
			path: '/login',
			component: Login
		},{
			path:'/main',
			component:Main,
			children:[
				{
					path:'/adminList',
					component:AdminList
				},{
					path:'/roleList',
					component:RoleList
				},{
					path:'/systemList',
					component:SystemList
				},{
					path:'/usersList',
					component:UsersList
				},{
					path: '/newsType',
					component: NewsType
				},{
					path: "/news",
					component: News
				},{
					path: "/link",
					component: Link
				},{
					path: "/comment",
					component: Comment
				},
			]

		},{
			path:'/reg',
			component:Reg
		},{
			path:'/createNews',
			component:CreateNews
		},{
			path: '/userCenter',
			component: UserCenter,
			children:[
				{
					path:"/userInfo",
					component:UserInfo
				},
				{
					path:"/updatePwd",
					component:UpdatePwd
				},
				{
					path:"/myComment",
					component:MyComment
				},
				{
					path:"/mySC",
					component:MySC
				},
				{
					path:"/myDZ",
					component:MyDZ
				},
				{
					path:"/mynews",
					component:Mynews
				}
			]
		}
	],
})

// 为路由对象， 添加 beforeBach 导航守卫
// to - 将要访问的页面地址, from - 从哪个页面访问的, next - 放行函数
rout.beforeEach((to, from, next) => {
	console.log(to.path)
	if(to.path == '/login' || to.path=='/index' || to.path=='/newsDetail' || to.path=="/"
		|| to.path == '/userlogin' || to.path == '/userreg'||to.path=='/userPswUpdate') {//如果用户访问的登录页， 直接放行
		return next();//放行,去到to.path
	}else if(to.path=="/userCenter" || to.path=="/userInfo" || to.path=="/updatePwd" || to.path=="/myComment"
		|| to.path=="/mySC"|| to.path == '/createNews'||to.path=="/myDZ"||to.path=="/mynews"){
		var nickName =  sessionStorage.getItem("name");
		if(nickName==null){
			ElementUI.Message({showClose: true,
				message: "请先登录后才能发布文章！",
				type: "error"
			})
			return next("/userlogin");
		}else{
			next();
		}
	}else{
		var account =  sessionStorage.getItem("account");//从浏览器中取出用户信息
		if(account == null) {//表示没有登录
			return next("/login");
		} else {//已经登录
			next();
		}
	}
})


//导出路由对象
export default rout;
